package com.chengyu.core.service.services.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.result.ServicesRelateResult;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.mapper.PmsServicesMapper;
import com.chengyu.core.mapper.PmsServicesRelateMapper;
import com.chengyu.core.model.PmsServicesExample;
import com.chengyu.core.model.PmsServicesRelate;
import com.chengyu.core.model.PmsServicesRelateExample;
import com.chengyu.core.service.services.ServicesRelateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ServicesRelateServiceImpl implements ServicesRelateService {

	@Autowired
	private PmsServicesRelateMapper servicesRelateMapper;
	@Autowired
	private PmsServicesMapper servicesMapper;

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void addServicesRelate(PmsServicesRelate relate) {
		if(relate.getId() == null) {
			servicesRelateMapper.insertSelective(relate);
		} else {
			servicesRelateMapper.updateByPrimaryKeySelective(relate);
		}
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void deleteServicesRelate(List<Integer> idList) {
		PmsServicesRelateExample example = new PmsServicesRelateExample();
		example.createCriteria().andIdIn(idList);
		servicesRelateMapper.deleteByExample(example);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void updateSort(Integer relateId, Integer sort) {
		PmsServicesRelate updateRelate = new PmsServicesRelate();
		updateRelate.setId(relateId);
		updateRelate.setSort(sort);
		servicesRelateMapper.updateByPrimaryKeySelective(updateRelate);
	}

	@Override
	public CommonPage<ServicesRelateResult> getServicesRelateList(Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);

		PmsServicesRelateExample example = new PmsServicesRelateExample();
		example.setOrderByClause("sort asc");
		List<PmsServicesRelate> list = servicesRelateMapper.selectByExample(example);
		List<ServicesRelateResult> relateResults = new ArrayList<>();
		for(PmsServicesRelate relate : list){
			ServicesRelateResult relateResult = new ServicesRelateResult();
			relateResult.setRelate(relate);

			List<Integer> servicesIds = Convert.convert(new TypeReference<List<Integer>>() {}, relate.getServicesIds().split(CommonConstant.DH_REGEX));
			PmsServicesExample servicesExample = new PmsServicesExample();
			servicesExample.setOrderByClause("sort asc");
			servicesExample.createCriteria().andIdIn(servicesIds);
			relateResult.setServicesList(servicesMapper.selectByExample(servicesExample));
			relateResults.add(relateResult);
		}

		PageInfo pageInfo = new PageInfo<>(list);
		pageInfo.setList(relateResults);
		return CommonPage.restPage(pageInfo);
	}
}